var xmlhttp;

function getTime(selectmenu){
	var chosenoption=selectmenu.options[selectmenu.selectedIndex];
	var category = chosenoption.value;
	
	xmlhttp=new XMLHttpRequest();
	if (xmlhttp==null){
		alert ("An error occurred. Please update your browser.");
		return;
	}
	var extracto = document.URL;
	extracto = extracto.replace("list", "getTime");
	var url = extracto + "?id=" + category + "&image="+Math.random();

	xmlhttp.onreadystatechange=updateTime;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function updateTime(){
	if (xmlhttp.readyState==4){
		var answer = xmlhttp.responseXML;
		var time = answer.getElementsByTagName("time")[0].childNodes[0].nodeValue;
		if(time>0){
			timeMessage = "If you don't want to start with the worst score of the current game, wait: "+ timer(time);
		}else{
			timeMessage = "";
		}		
		
		document.getElementById("categoryTime").innerHTML = timeMessage;
	}
}



window.onload=getTime(document.getElementById("categoryCombo"));


function two(x) {
	return ((x>9)?"":"0")+x
}

function timer(sec) {
	var min = Math.floor(sec/60)
	sec = sec % 60
	t = two(sec)
	
	var hr = Math.floor(min/60)
	min = min % 60
	t = two(min) + ":" + t
	
	var day = Math.floor(hr/24)
	hr = hr % 24
	t = two(hr) + ":" + t
	if(day!="0")
		t = day + " day" + (day>1? "s ":" ") + t
	
	return t
}
